﻿package com.hutra.utils
{
	import flash.display.*;
	import com.temple.core.CoreMovieClip;
	import com.temple.destruction.Destructor;
	import flash.events.MouseEvent;
	public class Cache
	{
		public function Cache():void
		{
		}
		public static function button(object:MovieClip,iscache:Boolean = true):void{
			try{
				object.cacheAsBitmap = iscache;
				object.gotoAndStop(1);
				object.addEventListener(MouseEvent.MOUSE_OVER,functionmouseover);
				object.addEventListener(MouseEvent.MOUSE_OUT,functionmouseout);
				object.addEventListener(MouseEvent.MOUSE_DOWN,functionmousedown);
				object.addEventListener(MouseEvent.MOUSE_UP,functionmouseup);
				object.addEventListener(MouseEvent.MOUSE_MOVE,functionmousemove);
			}catch (e:Error) {
				trace("button:",e);
			}
		}
		
		public static function bgbutton(object:MovieClip,iscache:Boolean = true):void {
			try{
				object.cacheAsBitmap = iscache;
				object.bgbutton.gotoAndStop(1);
				object.addEventListener(MouseEvent.MOUSE_OVER,functionmouseovers);
				object.addEventListener(MouseEvent.MOUSE_OUT,functionmouseouts);
				object.addEventListener(MouseEvent.MOUSE_DOWN,functionmousedowns);
				object.addEventListener(MouseEvent.MOUSE_UP,functionmouseups);
				object.addEventListener(MouseEvent.MOUSE_MOVE,functionmousemoves);
			}catch(e:Error){}
		}
		public static function bgputing(object:MovieClip,iscache:Boolean = true):void {
			try{
				object.cacheAsBitmap = iscache;
				object.bgbutton.gotoAndStop(1);
				object.bgleft.gotoAndStop(1);
				object.bgright.gotoAndStop(1);
				object.addEventListener(MouseEvent.MOUSE_OVER,functionmouseoversi);
				object.addEventListener(MouseEvent.MOUSE_OUT,functionmouseoutsi);
				object.addEventListener(MouseEvent.MOUSE_DOWN,functionmousedownsi);
				object.addEventListener(MouseEvent.MOUSE_UP,functionmouseupsi);
				object.addEventListener(MouseEvent.MOUSE_MOVE,functionmousemovesi);
			}catch(e:Error){}
		}
		public static function dibutton(object:MovieClip,iscache:Boolean = true):void {
			try{
				object.cacheAsBitmap = iscache;
				object.gotoAndStop(4);
				object.removeEventListener(MouseEvent.MOUSE_OVER,functionmouseover);
				object.removeEventListener(MouseEvent.MOUSE_OUT,functionmouseout);
				object.removeEventListener(MouseEvent.MOUSE_DOWN,functionmousedown);
				object.removeEventListener(MouseEvent.MOUSE_UP,functionmouseup);
				object.removeEventListener(MouseEvent.MOUSE_MOVE,functionmousemove);
			}catch(e:Error){}
		}
		public static function disbutton(object:MovieClip,iscache:Boolean = true):void {
			try{
				object.cacheAsBitmap = iscache;
				object.gotoAndStop(4);
				object.removeEventListener(MouseEvent.MOUSE_OVER,functionmouseovers);
				object.removeEventListener(MouseEvent.MOUSE_OUT,functionmouseouts);
				object.removeEventListener(MouseEvent.MOUSE_DOWN,functionmousedowns);
				object.removeEventListener(MouseEvent.MOUSE_UP,functionmouseups);
				object.removeEventListener(MouseEvent.MOUSE_MOVE,functionmousemoves);
			}catch(e:Error){}
		}
		public static function mcvisible(obj:MovieClip,isvisible:Boolean):void {
			obj.visible = isvisible;
		}
		public static function remove(obj:MovieClip):void {
			if (obj == null) return;
			var n:int = obj.numChildren;
			while (n--)
			{
				try
				{
					obj.removeChildAt(n);
				}
				catch (e:Error)
				{
					trace("Error removeAllObject");
				}
			}
		}
		public static function deletes(obj:*):void{
			try
			{
				Destructor.destruct(obj);
			}
			catch (e:Error)
			{
				trace("Cache undelete!");
			}
		}
		public static function getClass(obj:Object):Class{
			return Object(obj).constructor;
		}
		public static function showVisible(bol:Boolean,... args):void{
			try
			{
				for (var i:int; i < args.length; i++)
				{
					Object(args[i]).visible = bol;
				}
			}
			catch (e:Error)
			{
				Track.log("Error showVisible in Cache");
			}
		}
		
		private static function functionmouseout(evt:MouseEvent):void{try{evt.currentTarget.gotoAndStop(1);}catch(e:Error){}}
		private static function functionmouseover(evt:MouseEvent):void{try{evt.currentTarget.gotoAndStop(2);}catch(e:Error){}}
		private static function functionmousedown(evt:MouseEvent):void{try{evt.currentTarget.gotoAndStop(3);}catch(e:Error){}}
		private static function functionmouseup(evt:MouseEvent):void{try{evt.currentTarget.gotoAndStop(2);}catch(e:Error){}}
		private static function functionmousemove(evt:MouseEvent):void{try{evt.currentTarget.gotoAndStop(2);}catch(e:Error){}}
		
		private static function functionmouseouts(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(1);}catch(e:Error){}}
		private static function functionmouseovers(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(2);}catch(e:Error){}}
		private static function functionmousedowns(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(3);}catch(e:Error){}}
		private static function functionmouseups(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(2);}catch(e:Error){}}
		private static function functionmousemoves(evt:MouseEvent):void { try { evt.currentTarget.bgbutton.gotoAndStop(2); } catch (e:Error) { }}
		
		private static function functionmouseoutsi(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(1); evt.currentTarget.bgleft.gotoAndStop(1); evt.currentTarget.bgright.gotoAndStop(1); }catch(e:Error){}}
		private static function functionmouseoversi(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(2); evt.currentTarget.bgleft.gotoAndStop(2); evt.currentTarget.bgright.gotoAndStop(2); }catch(e:Error){}}
		private static function functionmousedownsi(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(3); evt.currentTarget.bgleft.gotoAndStop(2); evt.currentTarget.bgright.gotoAndStop(2); }catch(e:Error){}}
		private static function functionmouseupsi(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(2); evt.currentTarget.bgleft.gotoAndStop(2); evt.currentTarget.bgright.gotoAndStop(2); }catch(e:Error){}}
		private static function functionmousemovesi(evt:MouseEvent):void{try{evt.currentTarget.bgbutton.gotoAndStop(2); evt.currentTarget.bgleft.gotoAndStop(2); evt.currentTarget.bgright.gotoAndStop(2); }catch(e:Error){}}
	
		public static function UUID(newLength:int = 1):String {
			var userAlphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
			var alphabet:Array = userAlphabet.split("");
			var alphabetLength:int = alphabet.length;
			var randomLetters:String = "";
			for (var i:int = 0; i < newLength; i++){
				randomLetters += alphabet[int(Math.floor(Math.random() * alphabetLength))];
			}
			return randomLetters;
		}
	}
}